package org.xlp.stopwatch;

/**
 * Description: 计时器 底层使用<code>System.currentTimeMillis()</code>实现
 * <br/>date: 2023/11/30 14:32
 *
 * @version 1.0
 * @author: xlp
 */
public class StopWatch {
    /**
     * 记录开始执行时间 单位：ms
     */
    private long startTime = 0L;

    /**
     * 记录结束时间 单位：ms
     */
    private long endTime = 0L;

    /**
     * 开始记录
     */
    public void start(){
        startTime = System.currentTimeMillis();
    }

    /**
     * 结束记录
     */
    public void stop(){
        endTime = System.currentTimeMillis();
    }

    /**
     * 获取总共所需时间描述
     * @return
     */
    public String stopAndFormatter(){
        stop();
        return toString();
    }

    @Override
    public String toString(){
        long needTime = endTime - startTime;
        if (needTime < 1000) return needTime + "ms";
        double needTimeD = needTime / 1000.0;
        if (needTimeD < 60) return needTimeD + "s";
        return needTimeD / 60 + "m";
    }
}
